package com.bookstore.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class JDBCUtils {
    public static Connection getConnection(String[] args) {
        DruidPooledConnection conn=null;
        try {
            DruidDataSource dataSource = new DruidDataSource();
            Properties pros = new Properties();
            InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
//            FileInputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResource(new File("E:\\javaWeb\\web\\src\\main\\resources\\druid.properties"));
            pros.load(resourceAsStream);
            dataSource.setDriverClassName(pros.getProperty("driverClass"));//驱动获取
            dataSource.setUrl(pros.getProperty("url"));//地  址
            dataSource.setUsername(pros.getProperty("username"));//用户名
            dataSource.setPassword(pros.getProperty("password"));//密  码
            conn = dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void release(Connection conn, PreparedStatement ps, ResultSet rs){
        try{
            if(conn!=null)conn.close();
            if (ps!=null)ps.close();
            if(rs!=null)rs.close();
        }catch (Exception e){
//            new RuntimeException(e);
            e.printStackTrace();
        }
    }
}
